package com.TropicalFlavor.service.impl;

import com.TropicalFlavor.dao.SaleGoodsDao;
import com.TropicalFlavor.dao.UserDao;
import com.TropicalFlavor.po.MarketGoods;
import com.TropicalFlavor.po.MarketUser;
import com.TropicalFlavor.service.userService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("userInformationService")
public class userServiceImpl implements userService
{
    @Resource
    private UserDao userDao;

    @Resource
    private SaleGoodsDao saleGoodsDao;

    @Override
    public boolean Register(MarketUser marketUser) {
        //往数据库（用户表）中添加信息
        return userDao.InsertUser(marketUser);
        //
    }

    @Override
    public boolean Login(String sNum, String Password, boolean IsAdmin)
    {
        String sID;
        if(IsAdmin)
        {
            sID = "ADMI" + sNum;
        }
        else
            sID = "NORM" + sNum;

        String UID = userDao.IsTrue(sID, Password);

        if(((UID != null )&&(UID.substring(0,4).equals("ADMI"))==IsAdmin))
        {
            return true;
        }
        //
        return false;
    }

    @Override
    public boolean ChangeInfo(MarketUser marketUser)
    {
        return userDao.ChangeInfo(marketUser);
    }

    @Override
    public boolean RemoveSaleGoods(String UID, MarketGoods marketGoods) {
        //从数据库（售卖物品表和物品表）中删除物品
        if (!saleGoodsDao.DeleteGoods(UID,marketGoods)) {
            return false;
        }
        if (!goodsDao.DeleteGoods(marketGoods)) {
            return false;
        }
        return true;
        //
    }

    @Override
    public MarketUser selectByPrimaryKey(String UID)
    {
        return this.userDao.selectByPrimaryKey(UID);
    }
}
